package slu.appli.wodtimer.stringparser;

import slu.appli.tools.parser.stringparser.StringParserException;
import slu.appli.tools.parser.stringparser.impl.AbstractEscapeChar;
import slu.appli.tools.parser.stringparser.interfaces.model.ModelBuilderInterface;
import slu.appli.wodtimer.DataContext;
import slu.appli.wodtimer.ItemTimer;

public class EscapeCharCloseGroup 
extends AbstractEscapeChar {

	public EscapeCharCloseGroup() {
		super(')',"closeGroup");
	}

	@Override
	public void processPreviousString(final String value, final ModelBuilderInterface modelBuilder) {
		DataContext context = (DataContext) modelBuilder.getDataContext();
		if (value != null) {
			context.addToElapse(context.convert(value));
			modelBuilder.addItem(new ItemTimer(context.consumeElapse()));
		}
		else if (context.getElapse() != 0) {
			modelBuilder.addItem(new ItemTimer(context.consumeElapse()));
		}
		modelBuilder.closeGroup();
	}

	@Override
	public void processNextString(final String value, final ModelBuilderInterface modelBuilder) {
		if (value != null) {
			throw new StringParserException("presence interdite");
		}
	}

}
